import {message} from 'antd'
import { fetchConfigData,saveConfigData } from '../services/api';

export default {
  namespace: 'setting',

  state: {
    config: {},
    loading: true,
  },

  effects: {
    *fetchConfig(_, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchConfigData);
      yield put({
        type: 'saveConfig',
        payload: response.data,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
      *save({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(saveConfigData,payload);
          if(response.status){
              message.success('保存成功')
              yield put({
                  type: 'fetchConfig'
              });
          }else {
              message.success('保存失败')
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
  },

  reducers: {
      saveConfig(state, {payload}) {
      return {
        ...state,
          config: payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
